<?php 
/**
 * @author [cnsecer] <[root@cnsecer.com]>
 * @version [1.0]
 */

namespace Home\Controller;
use Think\Controller;
Class CommonController extends Controller{
	public $c,$uid; 
	Public function _initialize(){

		//如果用户使用账号登陆
		if(isset($_SESSION['id'])){ 
			$db = M('user');
            $result = $db->where(array('id'=>session('id')))->select();
            $_SESSION['token']['access_token'] = $result[0]['access_token'];  //从数据库取出令牌  
			if(!isset($_SESSION['token']['access_token'])) { //如果用户没有绑定令牌，跳到主页让其绑定
				redirect(__APP__.'/User');
			}
		}
		//如果用户使用微博账号登陆
		if(!isset($_SESSION['id'])){  
			if(!isset($_SESSION['token']['access_token'])){
				redirect(__APP__.'/User');
			}
			//根据令牌获取用户本地账号信息 并存到session
			$db = M('user');
            $user = $db->where(array('access_token'=>$_SESSION['token']['access_token']))->select(); 
			session("id",$user[0]['id']);
			session("username",$user[0]['username']);
			session("logintime",date('Y-m-d H:i:s'),$user[0]['logintime']);
			session("loginip",$user[0]['loginip']);

		}
		//初始化用户UID和类
        import('Util.saetv2#ex',APP_PATH);
        //创建对象
        $this->c = new \Util\SaeTClientV2( C('WB_AKEY') , C('WB_SKEY') , $_SESSION['token']['access_token'] );
        $c = $this->c;
        $uid_get = $c->get_uid();
        //设置UID
        $this->uid = $uid_get['uid']; 

        
	}
}


